<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务，帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件，不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\jia\Dengji as DengjiModel;
use app\api\model\Order as OrderModel;
use app\api\service\Order as OrderService;
use app\api\service\User as UserService;
use app\common\service\qrcode\Extract as ExtractQRcode;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;

/**
 * 我的订单控制器
 * Class Order
 * @package app\api\controller
 */
class Order extends Controller {

		/**
	 * 发票订单列表
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function listAll(): Json {
		$model = new OrderModel;
		$list = $model->getListFapiao();
		return $this->renderSuccess(compact('list'));
	}
		/**
	 * 陪护师订单列表
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function listClerk(string $dataType): Json {
		$model = new OrderModel;
		$list = $model->getListClerk($dataType);
		return $this->renderSuccess(compact('list'));
	}
		/**
	 * 陪诊订单列表
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function listRunner(string $dataType): Json {
		$model = new OrderModel;
		$list = $model->getListRunner($dataType);
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 司机订单列表
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function listDriver(string $dataType): Json {
		$model = new OrderModel;
		$list = $model->getListDriver($dataType);
		return $this->renderSuccess(compact('list'));
	}

	/**
	 * 主管订单列表
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function listManage(string $dataType,string $keyword=''): Json {
		$model = new OrderModel;
		$list = $model->getListManage($dataType,$keyword);
		$bedFee=3;
		return $this->renderSuccess(compact('list','bedFee'));
	}
/**
	 * 主管订单列表-派单
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function listPaidan(string $dataType,string $keyword=''): Json {
		$model = new OrderModel;
		$list = $model->getListPaidan($dataType,$keyword);
		return $this->renderSuccess(compact('list'));
	}
	/**
	 * 我的订单列表
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function list(string $dataType): Json {
		$model = new OrderModel;
		$list = $model->getList($dataType);
		return $this->renderSuccess(compact('list'));
	}
	/**
	 * 我的订单列表-商城
	 * @param string $dataType 订单类型
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DbException
	 */
	public function listShangcheng(string $dataType): Json {
		$model = new OrderModel;
		$list = $model->getListShangcheng($dataType);
		return $this->renderSuccess(compact('list'));
	}
	public function getDengjiDetail(int $orderId): Json {
		// 订单详情
		$model = Db::name('jia_order_check')->where('order_id', $orderId)->find();

		if ($model) {
			$content = json_decode($model['content'], true);
			$label = json_decode($model['label'], true);
			$eatArr = explode(';', $content[11]);
		} else {
			$content = [];
			$label = [];
			$eatArr = [];
		}

		return $this->renderSuccess([
			'order' => $model, // 订单详情
			'label' => $label,
			'content' => $content,
			'eatArr' => $eatArr,

		]);
	}
	/**
	 * 订单详情信息
	 * @param int $orderId 订单ID
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function detail(int $orderId): Json {
		// 订单详情
		$model = OrderModel::getUserOrderDetail($orderId);

		return $this->renderSuccess([
			'order' => $model, // 订单详情
			'bedFee'=>3

		]);
	}
	//根据时间结算，获取金额
	public function detailTime(int $orderId, $date = ''): Json {
		// 订单详情
		$model = OrderModel::getUserOrderDetail($orderId);
		//计算时间

		if (!empty($date)) {
			$end_time = strtotime($date);
			$sub_time = $end_time - $model['peihu_time'];
			if ($sub_time <= 0) {
				return $this->renderError('请输入正确时间');
			}
			$yu = round($sub_time / 86400);

			if ($yu > 0) {
				$model['day'] = $yu . '天';
				$hour = $sub_time - $yu * 86400;
				$hour = $hour / 3600;
				$hour = round($hour, 1);

				$hour = $hour . '时';

				$model['day'] .= $hour;
			} else {
				$model['day'] = $hour . '时';
			}

		} else {
			if ($model['hour_type'] == 10) {
				$end_time = $model['peihu_time'] + 86400 * ($model['day'] - 1) + 12 * 60 * 60;
			} else {
				$end_time = $model['peihu_time'] + 86400 * $model['day'];
			}

			$model['day'] = $model['day'] . '天';

		}

		$model['end_time'] = Date('Y-m-d H:i', $end_time);
		$model['end_time_label'] = Date('m-d H:i', $end_time);

		$model['start_time'] = Date('m-d H:i', $model['peihu_time']);

		$model['total_pay_price'] = $model['total_price'];
		$model['refund_price'] = 0;
		$model['pay_price'] = 0;
		$model['goods_price'] = $model['goods'][0]['goods_price'];

		return $this->renderSuccess([
			'order' => $model, // 订单详情

		]);
	}
	/**
	 * 获取物流跟踪信息
	 * @param int $orderId 订单ID
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function express(int $orderId): Json {
		$service = new OrderService;
		$express = $service->express($orderId);
		return $this->renderSuccess(compact('express'));
	}

	/**
	 * 取消订单
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function cancel(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->cancel()) {
			return $this->renderSuccess($model->getMessage());
		}
		return $this->renderError($model->getError() ?: '订单取消失败');
	}
	/**
	 * 取消订单--服务
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function cancel2(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->cancel2()) {
			return $this->renderSuccess($model->getMessage());
		}
		return $this->renderError($model->getError() ?: '订单取消失败');
	}
	/**
	 * 更新停车信息
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function carSave(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->carSave($this->postForm())) {
			return $this->renderSuccess('更新成功');
		}
		return $this->renderError($model->getError());
	}
	/**
	 * 接车
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function jieche(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->jieche($this->postForm())) {
			return $this->renderSuccess('接车成功');
		}
		return $this->renderError($model->getError());
	}
	/**
	 * 接单
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function jiedan(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->jiedan($this->postForm())) {
			return $this->renderSuccess('接单成功');
		}
		return $this->renderError($model->getError());
	}
		/**
	 * 陪诊-完成
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function complateRunner(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->complateRunner($this->postForm())) {
			return $this->renderSuccess('处理成功');
		}
		return $this->renderError($model->getError());
	}
	/**
	 * 主管完成
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function complate(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->complate($this->postForm())) {
			return $this->renderSuccess('处理成功');
		}
		return $this->renderError($model->getError());
	}

	/**
	 * 司机确认时间
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function complatePark(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->complatePark($this->postForm())) {
			return $this->renderSuccess('处理成功');
		}
		return $this->renderError($model->getError());
	}
	/**
	 * 楼栋信息变更-主管
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function buildChange(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);

		$model->buildChange($this->postForm());
		return $this->renderSuccess('信息变更成功');

		return $this->renderError($model->getError());
	}
	/**
	 * 病情登记
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function dengji(int $orderId): Json {
		$model = DengjiModel::detail(['order_id' => $orderId]);
		if ($model) {

			if ($model->edit($this->postForm())) {
				return $this->renderSuccess('登记成功');
			}
		} else {
			$model = new DengjiModel;
			if ($model->add($orderId, $this->postForm())) {
				return $this->renderSuccess('登记成功');
			}
		}

		return $this->renderError($model->getError());
	}
		/**
	 * 主管修改价格
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function editPrice(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->editPrice($this->postForm())) {
			return $this->renderSuccess('修改成功');
		}
		return $this->renderError($model->getError());
	}
	/**
	 * 用户续费
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function xufeiUser(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->xufeiUser($this->postForm())) {
			return $this->renderSuccess('续费成功');
		}
		return $this->renderError($model->getError());
	}
	/**
	 *主管续费
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function xufei(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->xufei($this->postForm())) {
			return $this->renderSuccess('续费成功');
		}
		return $this->renderError($model->getError());
	}
				/**
	 * 陪护-派单
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function paidanPeihu(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->paidanPeihu($this->postForm())) {
			return $this->renderSuccess('派单成功');
		}
		return $this->renderError($model->getError());
	}
			/**
	 * 陪诊-派单
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function paidanRunner(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->paidanRunner($this->postForm())) {
			return $this->renderSuccess('派单成功');
		}
		return $this->renderError($model->getError());
	}
		/**
	 * 泊车-派单
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function paidanPark(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->paidanPark($this->postForm())) {
			return $this->renderSuccess('派单成功');
		}
		return $this->renderError($model->getError());
	}
	/**
	 * 确认收货
	 * @param int $orderId
	 * @return Json
	 * @throws BaseException
	 */
	public function receipt(int $orderId): Json {
		$model = OrderModel::getDetail($orderId);
		if ($model->receipt()) {
			return $this->renderSuccess('确认收货成功');
		}
		return $this->renderError($model->getError());
	}

	/**
	 * 获取当前用户待处理的订单数量
	 * @return Json
	 * @throws BaseException
	 */
	public function todoCounts(): Json {
		$model = new OrderModel;
		$counts = $model->getTodoCounts();
		return $this->renderSuccess(compact('counts'));
	}

	/**
	 * 获取自提核销二维码
	 * @param int $orderId
	 * @param string $channel 二维码渠道(小程序码、h5码)
	 * @return Json
	 * @throws BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function extractQrcode(int $orderId, string $channel = 'H5'): Json {
		// 订单详情
		$order = OrderModel::getDetail($orderId);
		// 判断是否为待核销订单
		if (!$order->checkExtractOrder($order)) {
			return $this->renderError($order->getError());
		}
		// 当前用户信息
		$userInfo = UserService::getCurrentLoginUser(true);
		// 创建二维码
		$Qrcode = new ExtractQRcode($userInfo, $orderId, $channel);
		return $this->renderSuccess(['qrcode' => $Qrcode->getImage()]);
	}

	/**
	 * 确认支付订单-代泊车-native支付
	 * @return Json
	 * @throws \cores\exception\BaseException
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 */
	public function submit(): Json {
		$data = $this->postForm();
		$RechargeService = new RechargeService;
		$data = $RechargeService->setMethod($data['method'])
			->setClient($data['client'])
			->orderPay($data['planId'], $data['customMoney'], $data['extra']);
		return $this->renderSuccess($data, $RechargeService->getMessage() ?: '下单成功');
	}
}
